package com.mdd.front.controller;

import com.mdd.common.core.PageResult;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.address.IUserAddressService;
import com.mdd.front.validate.address.UserAddressPageParam;
import com.mdd.front.validate.address.UserAddressParam;
import com.mdd.front.vo.address.UserAddressDetailVo;
import com.mdd.front.vo.address.UserAddressListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户地址管理
 */
@Api(tags = "用户地址管理")
@RestController
@RequestMapping("api/address")
public class UserAddressController {

    @Resource
    IUserAddressService iUserAddressService;

    /**
     * 用户地址列表
     *
     * @param params 搜索参数
     * @return Object
     */
    @ApiOperation(value = "用户地址管理 - 列表")
    @GetMapping("/list")
    public PageResult<UserAddressListVo> list(UserAddressPageParam params) {
        params.setUserId(Long.valueOf(LikeFrontThreadLocal.getUserId()));
        return iUserAddressService.list(params);
    }

    /**
     * 用户地址详情
     *
     * @param id 主键ID
     * @return Object
     */
    @ApiOperation(value = "用户地址管理 - 详情")
    @GetMapping("/detail")
    public UserAddressDetailVo detail(@RequestParam("id") Long id) {
        return iUserAddressService.detail(id);
    }

    /**
     * 用户地址新增
     *
     * @param userAddressParam 参数
     * @return Object
     */
    @ApiOperation(value = "用户地址管理 - 添加")
    @PostMapping("/add")
    public void add(@Validated(value = UserAddressParam.create.class) @RequestBody UserAddressParam userAddressParam) {
        userAddressParam.setUserId(Long.valueOf(LikeFrontThreadLocal.getUserId()));
        iUserAddressService.add(userAddressParam);
    }

    /**
     * 用户地址编辑
     *
     * @param userAddressParam 参数
     * @return Object
     */
    @ApiOperation(value = "用户地址管理 - 修改")
    @PostMapping("/edit")
    public void edit(@Validated(value = UserAddressParam.update.class) @RequestBody UserAddressParam userAddressParam) {
        iUserAddressService.edit(userAddressParam);
    }

    /**
     * 用户地址删除
     *
     * @return Object
     */
    @ApiOperation(value = "用户地址管理 - 删除")
    @GetMapping("/del")
    public void del(@RequestParam("id") Long id) {
        iUserAddressService.del(id);
    }

}
